/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.sys.utils;

import java.util.List;

import cn.knet.sp.ncrm.common.utils.CacheUtils;
import cn.knet.sp.ncrm.common.utils.SpringContextHolder;
import cn.knet.sp.ncrm.common.utils.StringParser;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.sys.producttype.dao.ProductTypeDao;
import cn.knet.sp.ncrm.modules.sys.producttype.entity.ProductType;

/**
 * 用户工具类
 *
 * @author ThinkGem
 * @version 2013-12-05
 */
public class ProductUtils {

	private static ProductTypeDao productTypeDao = SpringContextHolder.getBean(ProductTypeDao.class);
	public static final String PRODUCT_CACHE = "productCache";
	public static final String PRODUCT_CACHE_NAME = "name_";
	public static final String PRODUCT_CACHE_VALUE = "value_";

	public static ProductType empty = null;

	/**
	 * 初始化空值对象
	 * @author zhangbin
	 * @date 2015 /08/26 16:27:22
	 */
	private static void initEmpty() {
		if(empty==null){
			ProductType emptyEntity = new ProductType();
			emptyEntity.setProductTypeValue("WU");
			emptyEntity.setProductTypeName("无");
			empty = emptyEntity;
		}
	}

	/**
	 * 根据中文名称转换为对应字符简称
	 * @return 取不到返回null
	 */
	public static String get(String name) {
		return getEntity(name).getProductTypeValue();
	}

	/**
	 * 根据中文名称转换为对应对象
	 * @return 取不到返回null
	 */
	public static ProductType getEntity(String name) {
		initEmpty();
		if(StringUtils.isBlank(name)){
			return empty;
		}else{
			name = StringUtils.trim(name);
			//name = name.replaceAll(" ","");//清除空格 (暂不清除效率太差)
			name = StringParser.QtoB(name);//全角转半角
			/*if(StringUtils.isBlank(name)){
				return empty; //本句不会发生,因为空字符串在blank情况下为false
			}*/
		}
		int nameCode = name.hashCode();
		ProductType productType = (ProductType) CacheUtils.get(PRODUCT_CACHE, PRODUCT_CACHE_NAME + nameCode);
		if (productType == null) {
			ProductType param = new ProductType();
			param.setProductTypeName(name);//注意此处条件,根据不同接口要留意
			productType = productTypeDao.getByEntity(param);
			if (productType == null) {
				return empty;
			}
			int valueCode = productType.getProductTypeName().hashCode();
			CacheUtils.put(PRODUCT_CACHE, PRODUCT_CACHE_NAME + nameCode, productType);
			CacheUtils.put(PRODUCT_CACHE, PRODUCT_CACHE_VALUE + valueCode, productType);
		}
		return productType;
	}

	public static ProductType getEntityByValue(String value) {
		initEmpty();
		if(StringUtils.isBlank(value)){
			return empty;
		}else{
			value = StringUtils.trim(value);
			if(StringUtils.isBlank(value)||"WU".equals(value)){
				return empty;
			}
		}
		int valueCode = value.hashCode();
		ProductType productType = (ProductType) CacheUtils.get(PRODUCT_CACHE, PRODUCT_CACHE_VALUE + valueCode);
		if (productType == null) {
			ProductType param = new ProductType();
			param.setProductTypeValue(value);//条件2选1
			productType = productTypeDao.getByEntity(param);
			if (productType == null) {
				return empty;
			}
			int nameCode = productType.getProductTypeName().hashCode();
			CacheUtils.put(PRODUCT_CACHE, PRODUCT_CACHE_VALUE + valueCode, productType);
			CacheUtils.put(PRODUCT_CACHE, PRODUCT_CACHE_NAME + nameCode, productType);
		}
		return productType;
	}

	public static List<ProductType> getEntityList(String status) {
		ProductType param = new ProductType();
		param.setStatus(status);
		return productTypeDao.findAllList(param);
	}

	public static void clearCache(ProductType productType) {
		int nameCode = productType.getProductTypeName().hashCode();
		int valueCode = productType.getProductTypeValue().hashCode();
		CacheUtils.remove(PRODUCT_CACHE, PRODUCT_CACHE_VALUE + valueCode);
		CacheUtils.remove(PRODUCT_CACHE, PRODUCT_CACHE_NAME + nameCode);
	}

	public static void clearCache() {
		CacheUtils.getCacheManager().getCache(PRODUCT_CACHE).removeAll();
	}
}
